using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.IO;
using System.Collections.Generic;
using Android.Content;

namespace HighFantasy
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont font;
        private DirectoryInfo directory;
        public List<Level> levels = new List<Level>();
        private Level currentlevel;

        public int levelIndex, numberofLevels;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";

            graphics.IsFullScreen = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 480;
            graphics.SupportedOrientations = DisplayOrientation.Portrait;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Content.Load<SpriteFont>("Fonts/Hud");


            LoadLevels();
            
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Exit();
            }

            if (currentlevel.Exitreached == true)
            {
                currentlevel = levels[currentlevel.levelIndex + 1];
                currentlevel.Exitreached = false;
            }
            else
                currentlevel.Update(gameTime);

            base.Update(gameTime);
        }

         private void LoadLevels()

        {

            int numberOfLevels = 3;
            // move to the next level
            levelIndex = 0;

            string levelPath = string.Format("Content/Maps/{0}.xml", levelIndex);
            using (Stream fileStream = TitleContainer.OpenStream(levelPath))

                currentlevel = new Level("0", fileStream, 0, Content);

            levels.Add(currentlevel);

            
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);

            Matrix cameraTransform = Matrix.CreateTranslation(new Vector3(-currentlevel.cameraPosition, 0));
            spriteBatch.Begin(SpriteSortMode.Deferred,
                  BlendState.AlphaBlend,
                  null, null, null, null,
                  cameraTransform);

            GraphicsDevice.Clear(Color.Black);


            currentlevel.Draw(gameTime, spriteBatch);

            spriteBatch.DrawString(font, "PlayerXY=" + currentlevel.player.Position.X + "  " + currentlevel.player.Position.Y + " " + currentlevel.GetCollision(currentlevel.player.CoordX, currentlevel.player.CoordY), new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(font, "PlayerData=" + currentlevel.player.data.Profile.Name + " " + currentlevel.player.data.Profile.Profession, new Vector2(0, 20), Color.White);
            spriteBatch.DrawString(font, "Accel=" + currentlevel.player.movement.ToString(), new Vector2(0, 40), Color.White);
            spriteBatch.DrawString(font, "Object=" + currentlevel.objects[0].localBounds.X + " " + currentlevel.objects[0].localBounds.Y, new Vector2(0, 60), Color.White);
            spriteBatch.DrawString(font, "Level=" + currentlevel.Height + " " + currentlevel.Width + " " + currentlevel.mapfile.Name, new Vector2(0, 80), Color.White);
            spriteBatch.DrawString(font, "Camera=" + currentlevel.cameraPositionX + " " + currentlevel.cameraPositionY, new Vector2(0, 100), Color.White);
            spriteBatch.DrawString(font, "Entity=" + currentlevel.enemies[0].entitydata.id + " " + currentlevel.enemies[0].entitydata.Name, new Vector2(0, 120), Color.White);
            spriteBatch.End();
            spriteBatch.End();

            base.Draw(gameTime);

            base.Draw(gameTime);
        }
    }
}
